﻿/****************************************************************************************
 * CmdProcess.cs
 * 
 * The Cmd.exe wrapper class
 * Asghar Panahy
 * 05-jan-2011
 * http://stylishdosbox.codeplex.com
 ****************************************************************************************/
using System.Diagnostics;

namespace WpfCmd
{
    /// <summary>
    /// Manages the commands being executed in a process
    /// <remarks>Use Execute method to direct a command line to input stream.</remarks>
    /// <remarks>Use the Error and Output streams to see the results</remarks>
    /// </summary>
    public class CmdProcess : Process
    {                
        /// <summary>
        /// Default constructor.
        /// Starts the CMD.exe as a background process, hidden window style
        /// </summary>
        public CmdProcess():base()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("CMD.exe");

            startInfo.RedirectStandardError = true;
            startInfo.RedirectStandardInput = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.ErrorDialog = false;
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;

            this.StartInfo = startInfo;                        
            this.EnableRaisingEvents = true;
            this.Start();
            this.WaitForExit(10); 
            this.BeginOutputReadLine();
            this.BeginErrorReadLine();
        }

        /// <summary>
        /// Directs a command line into the Cmd.exe process
        /// </summary>
        /// <param name="command"></param>
        public void Execute(string command)
        {
            this.StandardInput.WriteLine(command);
            this.StandardInput.Flush();
        }


        private void StartProcess()
        {
           
        }
    }
}
